package com.mvc.model;

import java.util.Date;

import com.common.utils.DateUtil;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.mvc.request.ReqCarQPage;

public class Car extends Model<Car> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3857163917936984196L;

	/***
	 * 表字段
	 * 
	 */
	public static interface TableField {
		String ID = "id";
		String TYPE = "type";
		String MAC = "mac";
		String PHONE = "phone";
		String CREATETIME = "createTime";
		String CONTENT = "content";
		String FATIME = "faTime";
		String STARTADDRESS = "startAddress";
		String ENDADDRESS = "endAddress";
		String MONEY = "money";
		String SIZEPERSON = "sizePerson";

	}

	public static final Car dao = new Car();

	public Page<Car> getNewPage(int pageNumber, int pageSize) {
		String select = "select *";
		String formwhere = "from car where 1=1 order by createTime desc";
		return dao.paginate(pageNumber, pageSize, select, formwhere);
	}

	// // 0 车找人 1 人找车
	public Page<Car> getPage(int pageNumber, int pageSize, int type) {
		String select = "select *";
		String formwhere = "from car where type=? order by createTime desc";
		return dao.paginate(pageNumber, pageSize, select, formwhere, type);
	}

	public Page<Car> getMyPage(int pageNumber, int pageSize, String mac) {
		String select = "select *";
		String formwhere = "from car where mac=? order by createTime desc";
		return dao.paginate(pageNumber, pageSize, select, formwhere, mac);
	}

	public Page<Car> queryPage(ReqCarQPage req) {
		StringBuffer formwhere = new StringBuffer("from car where 1=1 ");

		if (req.getType() == 1)
		{
			// 乘客找车
			formwhere.append(" and  type=").append(req.getType());
			
			if (StrKit.notBlank(req.getSizePerson()))
			{
				formwhere.append(" and sizePerson>=").append(req.getSizePerson());
			}
			
		}else if (req.getType() == 0)
		{
			formwhere.append(" and  type=").append(req.getType());
			
			if (StrKit.notBlank(req.getSizePerson()))
			{
				formwhere.append(" and sizePerson<=").append(req.getSizePerson());
			}
		}

		Date faDate = req.getFaTime();
		if (faDate != null) {
			formwhere.append(" and faTime >='").append(DateUtil.getDateTime_Y_M_D_H_0(faDate)).append("'");
		}
		
		if (StrKit.notBlank(req.getAddress())) 
		{
			String ads[] = req.getAddress().split("-");
			if (ads != null) {
				formwhere.append(" and ");
				if (ads.length > 1) {
					formwhere.append(" startAddress like '%").append(ads[0]).append("%'");
					formwhere.append(" and endAddress like '%").append(ads[1]).append("%'");
					
				} else if (ads.length == 1) {
					formwhere.append(" startAddress like '%").append(ads[0]).append("%'");
					formwhere.append(" or endAddress like '%").append(ads[0]).append("%'");
				} else {
					formwhere.append(" startAddress like '%").append(req.getAddress()).append("%'");
					formwhere.append(" or endAddress like '%").append(ads[0]).append("%'");
				}
			}
		}
		
		formwhere.append(" order by faTime asc");
		System.out.println(formwhere.toString());
		return dao.paginate(req.getNowPage(), req.getPageSize(), "select *",
				formwhere.toString());
	}
}
